package com.dg.controller;

import com.dg.common.ServerResponse;
import com.dg.dto.RoleAclModuleTreeDto;
import com.dg.dto.UserTransferDto;
import com.dg.param.RoleParam;
import com.dg.pojo.Role;
import com.dg.service.RoleService;
import com.dg.service.RoleUserService;
import com.dg.service.UserService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role/")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleUserService roleUserService;
    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public ServerResponse<List<Role>> list(){
        return ServerResponse.createBySuccess(roleService.list());
    }

    @RequestMapping("save")
    public ServerResponse<List<Role>> save(RoleParam param){
        roleService.save(param);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("update")
    public ServerResponse update(RoleParam param){
        roleService.update(param);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("delete")
    public ServerResponse delete(Integer id){
        roleService.delete(id);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("roleTree")
    public ServerResponse<List<RoleAclModuleTreeDto>> roleTree(Integer id){
        return ServerResponse.createBySuccess(roleService.roleTree(id));
    }

    @RequestMapping("changeAcl")
    public ServerResponse changeAcl(Integer roleId, String ids){
        roleService.changeAcl(roleId, ids);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping("roleUser")
    public ServerResponse<Map<String,Object>> roleUser(Integer roleId){
        List<Integer> selectUserIdList = roleUserService.selectUser(roleId);
        List<UserTransferDto> userList = userService.getAll();
        Map<String,Object> map = Maps.newHashMap();
        map.put("userList", userList);
        map.put("selectUserIdList", selectUserIdList);
        return ServerResponse.createBySuccess(map);
    }

    @RequestMapping("changeRoleUser")
    public ServerResponse changeRoleUser(Integer roleId, String ids){
        roleService.changeRoleUser(roleId, ids);
        return ServerResponse.createBySuccess();
    }

}
